{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.7---时间模块：datetime\n",
    "## datetime模块，主要掌握：datetime.date(), datetime.datetime(), datetime.timedelta()\n",
    "## 日期解析方法：parser.parse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-04-02 \t <class 'datetime.date'>\n",
      "2018-04-02 \t <class 'str'>\n",
      "2016-06-01\n"
     ]
    }
   ],
   "source": [
    "# datetime.date : date对象\n",
    "\n",
    "import datetime          # 也可以写成 from datetime import date\n",
    "\n",
    "today = datetime.date.today()\n",
    "print(today,'\\t',type(today))\n",
    "print(str(today),'\\t',type(str(today)))\n",
    "# datetime.date.today()   返回今日\n",
    "# 输出格式为 date类\n",
    "\n",
    "t = datetime.date(2016,6,1)\n",
    "print(t)\n",
    "# (年，月，日)--->直接得到当时日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-04-02 19:34:33.065408 \t <class 'datetime.datetime'>\n",
      "2018-04-02 19:34:33.065408 \t <class 'str'>\n",
      "-882 days, 12:44:33\n"
     ]
    }
   ],
   "source": [
    "# datetime.datetime : datetime对象\n",
    "\n",
    "now = datetime.datetime.now()\n",
    "print(now,'\\t',type(now))\n",
    "print(str(now),'\\t',type(str(now)))\n",
    "# .now()方法，输出当前时间\n",
    "# 输出格式为 datetime类\n",
    "# 可以通过str()转化为字符串\n",
    "\n",
    "t1 = datetime.datetime(2016,6,1)\n",
    "t2 = datetime.datetime(2014,1,1,12,44,33)\n",
    "# (年，月，日，时，分，秒)--->至少要输入年、月、日\n",
    "\n",
    "print(t2-t1)\n",
    "# 相减得到时间差---timedelta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-04-02 19:34:34.015738\n",
      "2018-04-01 19:34:34.015738\n",
      "2018-03-26 19:34:34.015738\n"
     ]
    }
   ],
   "source": [
    "# datetime.timedelta : 时间差\n",
    "\n",
    "today = datetime.datetime.today()        # datetime.datetime也有today()方法\n",
    "yestoday = today - datetime.timedelta(1)\n",
    "print(today)\n",
    "print(yestoday)\n",
    "print(today - datetime.timedelta(7))\n",
    "# 时间差主要用作时间的加减法，相当于可以被识别的“时间差值”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-03-31 00:00:00 \t <class 'datetime.datetime'>\n",
      "2000-01-01 00:00:00 \n",
      " 2014-05-01 00:00:00 \n",
      " 2014-01-05 00:00:00 \n",
      " 2014-01-22 00:00:00 \n",
      " 1997-01-31 22:45:00\n"
     ]
    }
   ],
   "source": [
    "# parser.parse : 日期字符串转换\n",
    "\n",
    "from dateutil.parser import parse\n",
    "\n",
    "date = '31-3-2018'\n",
    "t = parse(date)\n",
    "print(t,'\\t',type(t))\n",
    "# 直接将string转换为datetime.datetime\n",
    "\n",
    "print(parse('2000-1-1'),'\\n',\n",
    "     parse('5/1/2014'),'\\n',\n",
    "     parse('5/1/2014', dayfirst = True),'\\n',  # 国际通用格式中，日在月之前，可以通过dayfirst来设置\n",
    "     parse('22/1/2014'),'\\n',\n",
    "     parse('Jan 31, 1997 10:45 PM'))\n",
    "# 各种格式可以解析，但无法支持中文   Note:此处务必要注意了！！！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.8---Pandas时刻数据：Timestamp()\n",
    "## 时刻数据代表时间点，是pandas的数据类型，是将值与时间点相关联的最基本的类型的时间序列数据\n",
    "## pandas.Timestamp()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-12-01 12:45:30 \t <class 'pandas._libs.tslib.Timestamp'>\n",
      "2018-03-31 00:00:00 \t <class 'pandas._libs.tslib.Timestamp'>\n",
      "2017-12-21 15:00:22\n"
     ]
    }
   ],
   "source": [
    "# pd.Timestamp()\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "date1 = datetime.datetime(2016,12,1,12,45,30)                   # 创建一个datetime.datetime\n",
    "date2 = '2018-3-31'                        # 创建一个字符串\n",
    "t1 = pd.Timestamp(date1)\n",
    "t2 = pd.Timestamp(date2)\n",
    "print(t1,'\\t',type(t1))\n",
    "print(t2,'\\t',type(t2))\n",
    "print(pd.Timestamp('2017-12-21 15:00:22'))\n",
    "# 直接生成pandas的时刻数据 → 时间戳\n",
    "# 数据类型为 pandas的Timestamp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-12-01 12:45:30 \t <class 'pandas._libs.tslib.Timestamp'>\n",
      "2018-03-31 00:00:00 \t <class 'pandas._libs.tslib.Timestamp'>\n",
      "DatetimeIndex(['2017-12-21', '2017-12-22', '2017-12-23'], dtype='datetime64[ns]', freq=None) \t <class 'pandas.core.indexes.datetimes.DatetimeIndex'>\n"
     ]
    }
   ],
   "source": [
    "# pd.to_datetime\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "date1 = datetime(2016,12,1,12,45,30)\n",
    "date2 = '2018-3-31' \n",
    "t1 = pd.to_datetime(date1)\n",
    "t2 = pd.to_datetime(date2)\n",
    "print(t1,'\\t',type(t1))\n",
    "print(t2,'\\t',type(t2))\n",
    "# pd.to_datetime()：如果是单个时间数据，转换成pandas的时刻数据，数据类型为Timestamp\n",
    "\n",
    "lst_date = [ '2017-12-21', '2017-12-22', '2017-12-23']\n",
    "t3 = pd.to_datetime(lst_date)\n",
    "print(t3,'\\t',type(t3))\n",
    "# 多个时间数据，将会转换为pandas的DatetimeIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[datetime.datetime(2017, 6, 1, 0, 0), datetime.datetime(2017, 7, 1, 0, 0), datetime.datetime(2017, 8, 1, 0, 0), datetime.datetime(2017, 9, 1, 0, 0), datetime.datetime(2017, 10, 1, 0, 0)]\n",
      "['2017-2-1', '2017-2-2', '2017-2-3', '2017-2-4', '2017-2-5', '2017-2-6']\n",
      "DatetimeIndex(['2017-06-01', '2017-07-01', '2017-08-01', '2017-09-01',\n",
      "               '2017-10-01'],\n",
      "              dtype='datetime64[ns]', freq=None)\n",
      "DatetimeIndex(['2017-02-01', '2017-02-02', '2017-02-03', '2017-02-04',\n",
      "               '2017-02-05', '2017-02-06'],\n",
      "              dtype='datetime64[ns]', freq=None)\n",
      "['2017-2-1' '2017-2-2' '2017-2-3' 'Hello World!' '2017-2-5' '2017-2-6'] \t <class 'numpy.ndarray'>\n",
      "DatetimeIndex(['2017-02-01', '2017-02-02', '2017-02-03', 'NaT', '2017-02-05',\n",
      "               '2017-02-06'],\n",
      "              dtype='datetime64[ns]', freq=None) \t <class 'pandas.core.indexes.datetimes.DatetimeIndex'>\n"
     ]
    }
   ],
   "source": [
    "# pd.to_datetime  --->多个时间数据转化为时间戳索引\n",
    "\n",
    "date1 = [datetime(2017,6,1),datetime(2017,7,1),datetime(2017,8,1),datetime(2017,9,1),datetime(2017,10,1)]\n",
    "date2 = ['2017-2-1','2017-2-2','2017-2-3','2017-2-4','2017-2-5','2017-2-6']\n",
    "print(date1)\n",
    "print(date2)\n",
    "t1 = pd.to_datetime(date1)\n",
    "t2 = pd.to_datetime(date2)\n",
    "print(t1)\n",
    "print(t2)\n",
    "# 多个时间数据转化为DatetimeIndex\n",
    "\n",
    "date3 = ['2017-2-1','2017-2-2','2017-2-3','Hello World!','2017-2-5','2017-2-6']\n",
    "t3 = pd.to_datetime(date3,errors = 'ignore')\n",
    "print(t3,'\\t',type(t3))\n",
    "# 当一组时间序列中夹杂其他格式数据，可用errors参数返回\n",
    "# errors = 'ignore':不可解析时返回原始输入，这里就是直接生成一般数组\n",
    "\n",
    "t4 = pd.to_datetime(date3,errors = 'coerce')\n",
    "print(t4,'\\t',type(t4))\n",
    "# errors = 'coerce':不可扩展，缺失值返回NaT（Not a Time），结果认为DatetimeIndex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.9---pandas时间戳索引：DatetimeIndex\n",
    "## 核心：pd.date_range()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2017-12-01', '2017-12-02', '2017-12-03', '2017-12-04',\n",
      "               '2017-12-05'],\n",
      "              dtype='datetime64[ns]', freq=None) \t <class 'pandas.core.indexes.datetimes.DatetimeIndex'>\n",
      "2017-12-01 00:00:00 \t <class 'pandas._libs.tslib.Timestamp'>\n",
      "2017-12-01    0.747851\n",
      "2017-12-02    0.169112\n",
      "2017-12-03    0.463788\n",
      "2017-12-04    0.869247\n",
      "2017-12-05    0.121155\n",
      "dtype: float64 \t <class 'pandas.core.series.Series'>\n",
      "DatetimeIndex(['2017-12-01', '2017-12-02', '2017-12-03', '2017-12-04',\n",
      "               '2017-12-05'],\n",
      "              dtype='datetime64[ns]', freq=None)\n"
     ]
    }
   ],
   "source": [
    "# pd.DatetimeIndex()与 TimeSeries时间序列\n",
    "\n",
    "rng = pd.DatetimeIndex(['2017-12-1','2017-12-2','2017-12-3','2017-12-4','2017-12-5'])\n",
    "print(rng,'\\t',type(rng))\n",
    "print(rng[0],'\\t',type(rng[0]))\n",
    "# 直接生成时间戳索引，支持str，datetime.datetime\n",
    "# 单个时间戳为TimeStamp,多个时间戳为DatetimeIndex\n",
    "\n",
    "st = pd.Series(np.random.rand(len(rng)),index = rng)\n",
    "print(st,'\\t',type(st))\n",
    "print(st.index)\n",
    "# 以DatetimeIndex为index的Series，是为TimeSeries，即为时间序列\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',\n",
      "               '2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',\n",
      "               '2017-01-09', '2017-01-10',\n",
      "               ...\n",
      "               '2017-09-22', '2017-09-23', '2017-09-24', '2017-09-25',\n",
      "               '2017-09-26', '2017-09-27', '2017-09-28', '2017-09-29',\n",
      "               '2017-09-30', '2017-10-01'],\n",
      "              dtype='datetime64[ns]', length=274, freq='D') \t <class 'pandas.core.indexes.datetimes.DatetimeIndex'>\n",
      "DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',\n",
      "               '2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',\n",
      "               '2017-01-09', '2017-01-10'],\n",
      "              dtype='datetime64[ns]', freq='D')\n",
      "DatetimeIndex(['2017-01-21 15:00:00', '2017-01-22 15:00:00',\n",
      "               '2017-01-23 15:00:00', '2017-01-24 15:00:00',\n",
      "               '2017-01-25 15:00:00', '2017-01-26 15:00:00',\n",
      "               '2017-01-27 15:00:00', '2017-01-28 15:00:00',\n",
      "               '2017-01-29 15:00:00', '2017-01-30 15:00:00'],\n",
      "              dtype='datetime64[ns]', freq='D')\n",
      "-----------------------------------------------------------\n",
      "DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',\n",
      "               '2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',\n",
      "               '2017-01-09', '2017-01-10'],\n",
      "              dtype='datetime64[ns]', name='hello world!', freq='D') \t <class 'pandas.core.indexes.datetimes.DatetimeIndex'>\n",
      "--------------------------------------------------------------\n",
      "DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'], dtype='datetime64[ns]', freq='D')\n",
      "DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04'], dtype='datetime64[ns]', freq='D')\n",
      "DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03'], dtype='datetime64[ns]', freq='D')\n",
      "-------\n",
      "DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04', '2017-01-05',\n",
      "               '2017-01-06'],\n",
      "              dtype='datetime64[ns]', freq='B')\n",
      "[Timestamp('2017-01-01 00:00:00', freq='D'), Timestamp('2017-01-02 00:00:00', freq='D'), Timestamp('2017-01-03 00:00:00', freq='D'), Timestamp('2017-01-04 00:00:00', freq='D'), Timestamp('2017-01-05 00:00:00', freq='D'), Timestamp('2017-01-06 00:00:00', freq='D'), Timestamp('2017-01-07 00:00:00', freq='D'), Timestamp('2017-01-08 00:00:00', freq='D'), Timestamp('2017-01-09 00:00:00', freq='D'), Timestamp('2017-01-10 00:00:00', freq='D')]\n"
     ]
    }
   ],
   "source": [
    "# pd.date_range()---日期范围：生成日期范围\n",
    "# 两种生成方式：1.start + end ；2.start/end + periods\n",
    "# 默认频率：day\n",
    "\n",
    "rng1 = pd.date_range('2017-1-1','2017-10-1',normalize = True)\n",
    "rng2 = pd.date_range(start = '2017-1-1',periods = 10)\n",
    "rng3 = pd.date_range(end = '2017-1-30 15:00:00',periods = 10)          # 增加了时、分、秒\n",
    "print(rng1,'\\t',type(rng1))\n",
    "print(rng2)\n",
    "print(rng3)\n",
    "print(\"-----------------------------------------------------------\")\n",
    "# 直接生成DatetimeIndex\n",
    "# pd.date_range(start=None, end=None, periods=None, freq='D', tz=None, normalize=False, name=None, closed=None, **kwargs)\n",
    "# start：开始时间\n",
    "# end：结束时间\n",
    "# periods：偏移量\n",
    "# freq：频率，默认天，pd.date_range()默认频率为日历日，pd.bdate_range()默认频率为工作日\n",
    "# tz：时区\n",
    "\n",
    "rng4 = pd.date_range(start = '2017-1-1 15:30',periods = 10,name = 'hello world!',normalize = True)\n",
    "print(rng4,'\\t',type(rng4))\n",
    "print(\"--------------------------------------------------------------\")\n",
    "# normalize：时间参数值正则化到午夜时间戳（这里最后就直接变成0:00:00，并不是15:30:00）\n",
    "# name：索引对象名称\n",
    "\n",
    "print(pd.date_range('20170101','20170104'))  # 20170101也可读取\n",
    "print(pd.date_range('20170101','20170104',closed = 'right'))\n",
    "print(pd.date_range('20170101','20170104',closed = 'left'))\n",
    "print('-------')\n",
    "# closed：默认为None的情况下，左闭右闭，left则左闭右开，right则左开右闭\n",
    "\n",
    "print(pd.bdate_range('20170101','20170107'))\n",
    "# pd.bdate_range()默认频率为工作日\n",
    "\n",
    "print(list(pd.date_range(start = '1/1/2017', periods = 10)))\n",
    "# 直接转化为list，元素为Timestamp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'], dtype='datetime64[ns]', freq='D')\n",
      "DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04'], dtype='datetime64[ns]', freq='B')\n",
      "DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 01:00:00',\n",
      "               '2017-01-01 02:00:00', '2017-01-01 03:00:00',\n",
      "               '2017-01-01 04:00:00', '2017-01-01 05:00:00',\n",
      "               '2017-01-01 06:00:00', '2017-01-01 07:00:00',\n",
      "               '2017-01-01 08:00:00', '2017-01-01 09:00:00',\n",
      "               '2017-01-01 10:00:00', '2017-01-01 11:00:00',\n",
      "               '2017-01-01 12:00:00', '2017-01-01 13:00:00',\n",
      "               '2017-01-01 14:00:00', '2017-01-01 15:00:00',\n",
      "               '2017-01-01 16:00:00', '2017-01-01 17:00:00',\n",
      "               '2017-01-01 18:00:00', '2017-01-01 19:00:00',\n",
      "               '2017-01-01 20:00:00', '2017-01-01 21:00:00',\n",
      "               '2017-01-01 22:00:00', '2017-01-01 23:00:00',\n",
      "               '2017-01-02 00:00:00'],\n",
      "              dtype='datetime64[ns]', freq='H')\n",
      "DatetimeIndex(['2017-01-01 12:00:00', '2017-01-01 12:01:00',\n",
      "               '2017-01-01 12:02:00', '2017-01-01 12:03:00',\n",
      "               '2017-01-01 12:04:00', '2017-01-01 12:05:00',\n",
      "               '2017-01-01 12:06:00', '2017-01-01 12:07:00',\n",
      "               '2017-01-01 12:08:00', '2017-01-01 12:09:00',\n",
      "               '2017-01-01 12:10:00'],\n",
      "              dtype='datetime64[ns]', freq='T')\n",
      "DatetimeIndex(['2017-01-01 12:00:00', '2017-01-01 12:00:01',\n",
      "               '2017-01-01 12:00:02', '2017-01-01 12:00:03',\n",
      "               '2017-01-01 12:00:04', '2017-01-01 12:00:05',\n",
      "               '2017-01-01 12:00:06', '2017-01-01 12:00:07',\n",
      "               '2017-01-01 12:00:08', '2017-01-01 12:00:09',\n",
      "               '2017-01-01 12:00:10'],\n",
      "              dtype='datetime64[ns]', freq='S')\n",
      "DatetimeIndex([       '2017-01-01 12:00:00', '2017-01-01 12:00:00.001000',\n",
      "               '2017-01-01 12:00:00.002000', '2017-01-01 12:00:00.003000',\n",
      "               '2017-01-01 12:00:00.004000', '2017-01-01 12:00:00.005000',\n",
      "               '2017-01-01 12:00:00.006000', '2017-01-01 12:00:00.007000',\n",
      "               '2017-01-01 12:00:00.008000', '2017-01-01 12:00:00.009000',\n",
      "               ...\n",
      "               '2017-01-01 12:00:09.991000', '2017-01-01 12:00:09.992000',\n",
      "               '2017-01-01 12:00:09.993000', '2017-01-01 12:00:09.994000',\n",
      "               '2017-01-01 12:00:09.995000', '2017-01-01 12:00:09.996000',\n",
      "               '2017-01-01 12:00:09.997000', '2017-01-01 12:00:09.998000',\n",
      "               '2017-01-01 12:00:09.999000',        '2017-01-01 12:00:10'],\n",
      "              dtype='datetime64[ns]', length=10001, freq='L')\n",
      "DatetimeIndex([       '2017-01-01 12:00:00', '2017-01-01 12:00:00.000001',\n",
      "               '2017-01-01 12:00:00.000002', '2017-01-01 12:00:00.000003',\n",
      "               '2017-01-01 12:00:00.000004', '2017-01-01 12:00:00.000005',\n",
      "               '2017-01-01 12:00:00.000006', '2017-01-01 12:00:00.000007',\n",
      "               '2017-01-01 12:00:00.000008', '2017-01-01 12:00:00.000009',\n",
      "               ...\n",
      "               '2017-01-01 12:00:09.999991', '2017-01-01 12:00:09.999992',\n",
      "               '2017-01-01 12:00:09.999993', '2017-01-01 12:00:09.999994',\n",
      "               '2017-01-01 12:00:09.999995', '2017-01-01 12:00:09.999996',\n",
      "               '2017-01-01 12:00:09.999997', '2017-01-01 12:00:09.999998',\n",
      "               '2017-01-01 12:00:09.999999',        '2017-01-01 12:00:10'],\n",
      "              dtype='datetime64[ns]', length=10000001, freq='U')\n",
      "DatetimeIndex(['2017-01-02', '2017-01-09', '2017-01-16', '2017-01-23',\n",
      "               '2017-01-30'],\n",
      "              dtype='datetime64[ns]', freq='W-MON')\n",
      "DatetimeIndex(['2017-01-09', '2017-02-13', '2017-03-13', '2017-04-10'], dtype='datetime64[ns]', freq='WOM-2MON')\n"
     ]
    }
   ],
   "source": [
    "# pd.date_range()---日期范围：频率(1)\n",
    "\n",
    "print(pd.date_range('2017/1/1','2017/1/4'))                    # 默认freq = 'D': 每日历日\n",
    "print(pd.date_range('2017/1/1','2017/1/4',freq = 'B'))        # B:每工作日\n",
    "print(pd.date_range('2017/1/1','2017/1/2',freq = 'H'))       # H: 每小时\n",
    "print(pd.date_range('2017/1/1 12:00','2017/1/1 12:10',freq = 'T'))   # T/MIN: 每分\n",
    "print(pd.date_range('2017/1/1 12:00:00','2017/1/1 12:00:10',freq = 'S'))     # S: 每秒\n",
    "print(pd.date_range('2017/1/1 12:00:00','2017/1/1 12:00:10',freq = 'L'))    # L: 每毫秒（千分之一秒）\n",
    "print(pd.date_range('2017/1/1 12:00:00','2017/1/1 12:00:10',freq = 'U'))   # U: 每微秒（百万分之一秒）\n",
    "\n",
    "print(pd.date_range('2017/1/1','2017/2/1',freq = 'W-MON'))\n",
    "# W-MON: 从指定星期几开始算起，每周\n",
    "# 星期几缩写：MON / TUE / WED / THU / FRI / SAT / SUN\n",
    "\n",
    "print(pd.date_range('2017/1/1,','2017/5/1',freq = 'WOM-2MON'))\n",
    "# WOM-2MON：每月的第几个星期几开始算，这里是每月第二个星期一"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2017-01-31', '2017-02-28', '2017-03-31', '2017-04-30',\n",
      "               '2017-05-31', '2017-06-30', '2017-07-31', '2017-08-31',\n",
      "               '2017-09-30', '2017-10-31', '2017-11-30', '2017-12-31'],\n",
      "              dtype='datetime64[ns]', freq='M')\n",
      "DatetimeIndex(['2017-03-31', '2017-06-30', '2017-09-30', '2017-12-31',\n",
      "               '2018-03-31', '2018-06-30', '2018-09-30', '2018-12-31',\n",
      "               '2019-03-31', '2019-06-30', '2019-09-30', '2019-12-31'],\n",
      "              dtype='datetime64[ns]', freq='Q-DEC')\n",
      "DatetimeIndex(['2017-12-31', '2018-12-31', '2019-12-31'], dtype='datetime64[ns]', freq='A-DEC')\n",
      "----------------------------------------------------------------\n",
      "DatetimeIndex(['2017-01-31', '2017-02-28', '2017-03-31', '2017-04-28',\n",
      "               '2017-05-31', '2017-06-30', '2017-07-31', '2017-08-31',\n",
      "               '2017-09-29', '2017-10-31', '2017-11-30', '2017-12-29'],\n",
      "              dtype='datetime64[ns]', freq='BM')\n",
      "DatetimeIndex(['2017-03-31', '2017-06-30', '2017-09-29', '2017-12-29',\n",
      "               '2018-03-30', '2018-06-29', '2018-09-28', '2018-12-31',\n",
      "               '2019-03-29', '2019-06-28', '2019-09-30', '2019-12-31'],\n",
      "              dtype='datetime64[ns]', freq='BQ-DEC')\n",
      "DatetimeIndex(['2017-12-29', '2018-12-31', '2019-12-31'], dtype='datetime64[ns]', freq='BA-DEC')\n",
      "---------------------------------------------------------------\n",
      "DatetimeIndex(['2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01',\n",
      "               '2017-05-01', '2017-06-01', '2017-07-01', '2017-08-01',\n",
      "               '2017-09-01', '2017-10-01', '2017-11-01', '2017-12-01',\n",
      "               '2018-01-01'],\n",
      "              dtype='datetime64[ns]', freq='MS')\n",
      "DatetimeIndex(['2017-03-01', '2017-06-01', '2017-09-01', '2017-12-01',\n",
      "               '2018-03-01', '2018-06-01', '2018-09-01', '2018-12-01',\n",
      "               '2019-03-01', '2019-06-01', '2019-09-01', '2019-12-01'],\n",
      "              dtype='datetime64[ns]', freq='QS-DEC')\n",
      "DatetimeIndex(['2017-12-01', '2018-12-01', '2019-12-01'], dtype='datetime64[ns]', freq='AS-DEC')\n",
      "---------------------------------------------------------------\n",
      "DatetimeIndex(['2017-01-02', '2017-02-01', '2017-03-01', '2017-04-03',\n",
      "               '2017-05-01', '2017-06-01', '2017-07-03', '2017-08-01',\n",
      "               '2017-09-01', '2017-10-02', '2017-11-01', '2017-12-01',\n",
      "               '2018-01-01'],\n",
      "              dtype='datetime64[ns]', freq='BMS')\n",
      "DatetimeIndex(['2017-03-01', '2017-06-01', '2017-09-01', '2017-12-01',\n",
      "               '2018-03-01', '2018-06-01', '2018-09-03', '2018-12-03',\n",
      "               '2019-03-01', '2019-06-03', '2019-09-02', '2019-12-02'],\n",
      "              dtype='datetime64[ns]', freq='BQS-DEC')\n",
      "DatetimeIndex(['2017-12-01', '2018-12-03', '2019-12-02'], dtype='datetime64[ns]', freq='BAS-DEC')\n",
      "---------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# pd.date_range()---日期范围：频率(2)\n",
    "\n",
    "print(pd.date_range('2017','2018',freq = 'M'))\n",
    "print(pd.date_range('2017','2020',freq = 'Q-DEC'))\n",
    "print(pd.date_range('2017','2020',freq = 'A-DEC'))\n",
    "print(\"----------------------------------------------------------------\")\n",
    "# M：每月最后一个日历日\n",
    "# Q-月：指定月为季度末，每个季度末最后一月的最后一个日历日\n",
    "# A-月：每年指定月份的最后一个日历日\n",
    "# 月缩写：JAN/FEB/MAR/APR/MAY/JUN/JUL/AUG/SEP/OCT/NOV/DEC\n",
    "# 所以Q-月只有三种情况：1-4-7-10,2-5-8-11,3-6-9-12\n",
    "\n",
    "print(pd.date_range('2017','2018',freq = 'BM'))\n",
    "print(pd.date_range('2017','2020',freq = 'BQ-DEC'))\n",
    "print(pd.date_range('2017','2020',freq = 'BA-DEC'))\n",
    "print(\"---------------------------------------------------------------\")\n",
    "# BM：每月最后一个工作日\n",
    "# BQ-月：指定月为季度末，每个季度末最后一月的最后一个工作日\n",
    "# BA-月：每年指定月份的最后一个工作日\n",
    "\n",
    "print(pd.date_range('2017','2018',freq = 'MS'))\n",
    "print(pd.date_range('2017','2020',freq = 'QS-DEC'))\n",
    "print(pd.date_range('2017','2020',freq = 'AS-DEC'))\n",
    "print(\"---------------------------------------------------------------\")\n",
    "# M：每月第一个日历日\n",
    "# Q-月：指定月为季度末，每个季度末最后一月的第一个日历日\n",
    "# A-月：每年指定月份的第一个日历日\n",
    "\n",
    "print(pd.date_range('2017','2018',freq = 'BMS'))\n",
    "print(pd.date_range('2017','2020',freq = 'BQS-DEC'))\n",
    "print(pd.date_range('2017','2020',freq = 'BAS-DEC'))\n",
    "print(\"---------------------------------------------------------------\")\n",
    "# BM：每月第一个工作日\n",
    "# BQ-月：指定月为季度末，每个季度末最后一月的第一个工作日\n",
    "# BA-月：每年指定月份的第一个工作日"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2017-01-01', '2017-01-08', '2017-01-15', '2017-01-22',\n",
      "               '2017-01-29'],\n",
      "              dtype='datetime64[ns]', freq='7D')\n",
      "DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 02:30:00',\n",
      "               '2017-01-01 05:00:00', '2017-01-01 07:30:00',\n",
      "               '2017-01-01 10:00:00', '2017-01-01 12:30:00',\n",
      "               '2017-01-01 15:00:00', '2017-01-01 17:30:00',\n",
      "               '2017-01-01 20:00:00', '2017-01-01 22:30:00'],\n",
      "              dtype='datetime64[ns]', freq='150T')\n",
      "DatetimeIndex(['2017-01-31', '2017-03-31', '2017-05-31', '2017-07-31',\n",
      "               '2017-09-30', '2017-11-30'],\n",
      "              dtype='datetime64[ns]', freq='2M')\n"
     ]
    }
   ],
   "source": [
    "# pd.date_range()---日期范围：复合频率\n",
    "\n",
    "print(pd.date_range('2017/1/1','2017/2/1',freq = '7D'))      # 7天\n",
    "print(pd.date_range('2017/1/1','2017/1/2',freq = '2h30min')) # 2小时30分钟\n",
    "print(pd.date_range('2017','2018',freq = '2M'))    # 2月，每月最后一个日历日"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-01-01    0.884265\n",
      "2017-01-02    0.004380\n",
      "2017-01-03    0.263643\n",
      "2017-01-04    0.652679\n",
      "Freq: D, dtype: float64\n",
      "2017-01-01 00:00:00    0.884265\n",
      "2017-01-01 04:00:00    0.884265\n",
      "2017-01-01 08:00:00    0.884265\n",
      "2017-01-01 12:00:00    0.884265\n",
      "2017-01-01 16:00:00    0.884265\n",
      "2017-01-01 20:00:00    0.884265\n",
      "2017-01-02 00:00:00    0.004380\n",
      "2017-01-02 04:00:00    0.004380\n",
      "2017-01-02 08:00:00    0.004380\n",
      "2017-01-02 12:00:00    0.004380\n",
      "2017-01-02 16:00:00    0.004380\n",
      "2017-01-02 20:00:00    0.004380\n",
      "2017-01-03 00:00:00    0.263643\n",
      "2017-01-03 04:00:00    0.263643\n",
      "2017-01-03 08:00:00    0.263643\n",
      "2017-01-03 12:00:00    0.263643\n",
      "2017-01-03 16:00:00    0.263643\n",
      "2017-01-03 20:00:00    0.263643\n",
      "2017-01-04 00:00:00    0.652679\n",
      "Freq: 4H, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# asfreq: 时期频率转换：\n",
    "\n",
    "ts = pd.Series(np.random.rand(4),\n",
    "              index = pd.date_range('20170101','20170104')\n",
    "              )\n",
    "print(ts)\n",
    "print(ts.asfreq('4H',method = 'ffill'))\n",
    "# 改变频率，这里是D改为4H\n",
    "# method：插值模式，None不插值，ffill用之前值填充，bfill用之后值填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-01-01    0.371257\n",
      "2017-01-02    0.290068\n",
      "2017-01-03    0.185959\n",
      "2017-01-04    0.339886\n",
      "Freq: D, dtype: float64\n",
      "2017-01-01         NaN\n",
      "2017-01-02         NaN\n",
      "2017-01-03    0.371257\n",
      "2017-01-04    0.290068\n",
      "Freq: D, dtype: float64\n",
      "2017-01-01    0.185959\n",
      "2017-01-02    0.339886\n",
      "2017-01-03         NaN\n",
      "2017-01-04         NaN\n",
      "Freq: D, dtype: float64\n",
      "---------------------------------------------------------\n",
      "2017-01-01         NaN\n",
      "2017-01-02   -0.218688\n",
      "2017-01-03   -0.358911\n",
      "2017-01-04    0.827746\n",
      "Freq: D, dtype: float64\n",
      "---------------------------------------------------------\n",
      "2017-01-03    0.371257\n",
      "2017-01-04    0.290068\n",
      "2017-01-05    0.185959\n",
      "2017-01-06    0.339886\n",
      "Freq: D, dtype: float64\n",
      "2017-01-01 00:02:00    0.371257\n",
      "2017-01-02 00:02:00    0.290068\n",
      "2017-01-03 00:02:00    0.185959\n",
      "2017-01-04 00:02:00    0.339886\n",
      "Freq: D, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# pd.date_range()---日期范围：超前/滞后数据\n",
    "\n",
    "ts = pd.Series(np.random.rand(4),\n",
    "              index = pd.date_range('20170101','20170104')\n",
    "              )\n",
    "print(ts)\n",
    "\n",
    "print(ts.shift(2))\n",
    "print(ts.shift(-2))\n",
    "print(\"---------------------------------------------------------\")\n",
    "# 正数：数值后移（滞后）；负数：数值前移（超前）\n",
    "\n",
    "per = ts / ts.shift(1) - 1\n",
    "print(per)\n",
    "print(\"---------------------------------------------------------\")\n",
    "# 计算变化百分比，这里计算：该时间戳与上一个时间戳相比，变化百分比\n",
    "\n",
    "print(ts.shift(2,freq = 'D'))\n",
    "print(ts.shift(2,freq = 'T'))\n",
    "# 加上freq参数：对时间戳进行位移，而不是对数值进行位移"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.10---pandas时期：Period\n",
    "## 核心：pd.Period()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-01 \t <class 'pandas._libs.period.Period'>\n",
      "2017-02\n",
      "2016-11\n",
      "2011\n"
     ]
    }
   ],
   "source": [
    "# pd.Period() 创建时期\n",
    "\n",
    "p = pd.Period('2017',freq = 'M')\n",
    "print(p,'\\t',type(p))\n",
    "# 生成一个从2017-01起，月为频率的时间构造器\n",
    "# pd.Period()参数：一个时间戳 + freq参数---> freq用于指明period的长度，时间戳则是说明该period在时间轴上面的位置\n",
    "\n",
    "print(p + 1)\n",
    "print(p - 2)\n",
    "print(pd.Period('2012',freq = 'A-DEC')-1)\n",
    "# 通过加减整数，将周期整体移动\n",
    "# 这里是按照月、年移动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05', '2011-06',\n",
      "             '2011-07', '2011-08', '2011-09', '2011-10', '2011-11', '2011-12',\n",
      "             '2012-01'],\n",
      "            dtype='period[M]', freq='M') \t <class 'pandas.core.indexes.period.PeriodIndex'>\n",
      "2011-01 \t <class 'pandas._libs.period.Period'>\n",
      "2011-01    0.974448\n",
      "2011-02    0.786362\n",
      "2011-03    0.437933\n",
      "2011-04    0.791063\n",
      "2011-05    0.240219\n",
      "2011-06    0.789412\n",
      "2011-07    0.130622\n",
      "2011-08    0.002802\n",
      "2011-09    0.093036\n",
      "2011-10    0.790399\n",
      "2011-11    0.762179\n",
      "2011-12    0.029475\n",
      "2012-01    0.981333\n",
      "Freq: M, dtype: float64 \t <class 'pandas.core.series.Series'>\n",
      "PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05', '2011-06',\n",
      "             '2011-07', '2011-08', '2011-09', '2011-10', '2011-11', '2011-12',\n",
      "             '2012-01'],\n",
      "            dtype='period[M]', freq='M')\n"
     ]
    }
   ],
   "source": [
    "# pd.period_range(): 创建时间范围\n",
    "\n",
    "prng = pd.period_range('1/1/2011','1/1/2012',freq = 'M')\n",
    "print(prng,'\\t',type(prng))\n",
    "print(prng[0],'\\t',type(prng[0]))\n",
    "# 数据格式为PeriodIndex，单个数值为Period\n",
    "\n",
    "ts = pd.Series(np.random.rand(len(prng)),index = prng)\n",
    "print(ts,'\\t',type(ts))\n",
    "print(ts.index)\n",
    "# 时间序列\n",
    "\n",
    "# Period('2011', freq = 'A-DEC')可以看成多个时间期的时间段中的游标\n",
    "# Timestamp表示一个时间戳，是一个时间截面；Period是一个时期，是一个时间段！！但两者作为index时区别不大"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017\n",
      "2017-01\n",
      "2017-12-31\n",
      "2017-01    0.530560\n",
      "2017-02    0.150625\n",
      "2017-03    0.480470\n",
      "2017-04    0.780167\n",
      "2017-05    0.973450\n",
      "Freq: M, dtype: float64 \t 13\n",
      "2017-01-01    0.689878\n",
      "2017-02-01    0.815536\n",
      "2017-03-01    0.141229\n",
      "2017-04-01    0.224788\n",
      "2017-05-01    0.556610\n",
      "Freq: D, dtype: float64 \t 13\n"
     ]
    }
   ],
   "source": [
    "# asfreq：频率转换\n",
    "\n",
    "p = pd.Period('2017',freq = 'A-DEC')\n",
    "print(p)\n",
    "print(p.asfreq('M',how = 'start'))        # 也可写 how = 's'\n",
    "print(p.asfreq('D',how = 'end'))          # 也可写 how = 'e'\n",
    "# 通过.asfreq(freq, method=None, how=None)方法转换成别的频率\n",
    "\n",
    "prng = pd.period_range('2017','2018',freq = 'M')\n",
    "ts1 = pd.Series(np.random.rand(len(prng)),index = prng)\n",
    "ts2 = pd.Series(np.random.rand(len(prng)),index = prng.asfreq('D',how = 'start'))\n",
    "print(ts1.head(),'\\t',len(ts1))\n",
    "print(ts2.head(),'\\t',len(ts2))\n",
    "# asfreq也可以转换TIMESeries的index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-01-31    0.446454\n",
      "2017-02-28    0.026414\n",
      "2017-03-31    0.307100\n",
      "2017-04-30    0.518259\n",
      "2017-05-31    0.802659\n",
      "Freq: M, dtype: float64\n",
      "2017-01    0.446454\n",
      "2017-02    0.026414\n",
      "2017-03    0.307100\n",
      "2017-04    0.518259\n",
      "2017-05    0.802659\n",
      "Freq: M, dtype: float64\n",
      "2017-01    0.740540\n",
      "2017-02    0.567075\n",
      "2017-03    0.402240\n",
      "2017-04    0.619862\n",
      "2017-05    0.380730\n",
      "Freq: M, dtype: float64\n",
      "2017-01-01    0.740540\n",
      "2017-02-01    0.567075\n",
      "2017-03-01    0.402240\n",
      "2017-04-01    0.619862\n",
      "2017-05-01    0.380730\n",
      "Freq: MS, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 时间戳与时期之间的转换：pd.to_period() , pd.to_timestamp()\n",
    "\n",
    "rng = pd.date_range('2017/1/1',periods = 10,freq = 'M')\n",
    "prng = pd.period_range('2017','2018',freq = 'M')\n",
    "\n",
    "ts1 = pd.Series(np.random.rand(len(rng)),index = rng)\n",
    "print(ts1.head())\n",
    "print(ts1.to_period().head())\n",
    "# 每月最后一日--->转化为每月！！！\n",
    "\n",
    "ts2 = pd.Series(np.random.rand(len(prng)),index = prng)\n",
    "print(ts2.head())\n",
    "print(ts2.to_timestamp().head())\n",
    "# 每月--->转化为每月第一天！！！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.11---时间序列 - 索引及切片\n",
    "## TimeSeries是Series的一个子类，所以Series索引及数据选取方面的方法基本一样\n",
    "## 同时TimeSeries通过时间序列有更便捷的方法做索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-01-01    0.618932\n",
      "2017-01-02    0.179809\n",
      "2017-01-03    0.350889\n",
      "2017-01-04    0.458495\n",
      "2017-01-05    0.754293\n",
      "Freq: D, dtype: float64\n",
      "0.6189318562135143\n",
      "2017-01-01    0.618932\n",
      "2017-01-02    0.179809\n",
      "Freq: D, dtype: float64\n",
      "-------------------------------------------------------------------\n",
      "0.17980920796732647\n",
      "0.3508889349140143\n",
      "0.12473415845791813\n",
      "0.4855510688501682\n",
      "-------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 索引\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "rng = pd.date_range('2017/1','2017/3')\n",
    "ts = pd.Series(np.random.rand(len(rng)),index = rng)\n",
    "print(ts.head())         # 默认显示前5条信息\n",
    "\n",
    "print(ts[0])\n",
    "print(ts[:2])\n",
    "print(\"-------------------------------------------------------------------\")\n",
    "# 基本下标位置索引\n",
    "\n",
    "print(ts['2017/1/2'])\n",
    "print(ts['20170103'])\n",
    "print(ts['1/10/2017'])\n",
    "print(ts[datetime(2017,1,20)])\n",
    "print(\"-------------------------------------------------------------------\")\n",
    "# 时间序列标签索引，支持各种时间字符串，以及datetime.datetime\n",
    "\n",
    "# 时间序列由于按照时间时间先后排序，故不用考虑顺序问题\n",
    "# 索引方法同样适用于Dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-01-05 00:00:00    0.799537\n",
      "2017-01-05 12:00:00    0.202168\n",
      "2017-01-06 00:00:00    0.123059\n",
      "2017-01-06 12:00:00    0.237609\n",
      "2017-01-07 00:00:00    0.489996\n",
      "2017-01-07 12:00:00    0.637901\n",
      "2017-01-08 00:00:00    0.567159\n",
      "2017-01-08 12:00:00    0.790545\n",
      "2017-01-09 00:00:00    0.484051\n",
      "2017-01-09 12:00:00    0.576202\n",
      "2017-01-10 00:00:00    0.648725\n",
      "2017-01-10 12:00:00    0.156410\n",
      "Freq: 12H, dtype: float64\n",
      "-----------------------------------------------------------------\n",
      "2017-02-01 00:00:00    0.685258\n",
      "2017-02-01 12:00:00    0.183595\n",
      "2017-02-02 00:00:00    0.561312\n",
      "2017-02-02 12:00:00    0.278216\n",
      "2017-02-03 00:00:00    0.672682\n",
      "Freq: 12H, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 切片\n",
    "\n",
    "rng = pd.date_range('2017/1','2017/3',freq = '12H')\n",
    "ts = pd.Series(np.random.rand(len(rng)),index = rng)\n",
    "\n",
    "print(ts['2017/1/5':'2017/1/10'])\n",
    "print(\"-----------------------------------------------------------------\")\n",
    "# 和Series按照index索引原理一样，也是末端包含\n",
    "\n",
    "print(ts['2017/2'].head())\n",
    "# 传入月，可以直接得到一个切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-01-01    0.767738\n",
      "2017-01-02    0.770289\n",
      "2017-01-03    0.281550\n",
      "2017-01-04    0.325139\n",
      "2017-01-01    0.997501\n",
      "2017-01-05    0.828130\n",
      "dtype: float64\n",
      "True False\n",
      "------------------------------------------------------------------------------------------\n",
      "2017-01-01    0.767738\n",
      "2017-01-01    0.997501\n",
      "dtype: float64 \t <class 'pandas.core.series.Series'>\n",
      "2017-01-04    0.325139\n",
      "dtype: float64 \t <class 'pandas.core.series.Series'>\n",
      "------------------------------------------------------------------------------------------\n",
      "2017-01-01    0.882619\n",
      "2017-01-02    0.770289\n",
      "2017-01-03    0.281550\n",
      "2017-01-04    0.325139\n",
      "2017-01-05    0.828130\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "：\n",
    "print(ts.groupby(level = 0).mean())\n",
    "# 通过groupby做分组，重复的值这里用平均值处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.12---时间序列---重采样\n",
    "## 将时间序列从一个频率转换为另一个频率的过程，且会有数据的结合\n",
    "\n",
    "## 降采样：高频数据--->低频数据，eg：以天为频率的数据转换为以月为频率的数据\n",
    "## 升采样：低频数据--->高频数据，eg：以年为频率的数据转换为以月为频率的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-01-01     0\n",
      "2017-01-02     1\n",
      "2017-01-03     2\n",
      "2017-01-04     3\n",
      "2017-01-05     4\n",
      "2017-01-06     5\n",
      "2017-01-07     6\n",
      "2017-01-08     7\n",
      "2017-01-09     8\n",
      "2017-01-10     9\n",
      "2017-01-11    10\n",
      "2017-01-12    11\n",
      "Freq: D, dtype: int32\n",
      "--------------------------------------------------------------------------\n",
      "DatetimeIndexResampler [freq=<5 * Days>, axis=0, closed=left, label=left, convention=start, base=0] \t <class 'pandas.core.resample.DatetimeIndexResampler'>\n",
      "2017-01-01    10\n",
      "2017-01-06    35\n",
      "2017-01-11    21\n",
      "dtype: int32 \t <class 'pandas.core.series.Series'>\n",
      "--------------------------------------------------------------------------\n",
      "2017-01-01     2.0\n",
      "2017-01-06     7.0\n",
      "2017-01-11    10.5\n",
      "dtype: float64 --->求平均值\n",
      "\n",
      "2017-01-01     4\n",
      "2017-01-06     9\n",
      "2017-01-11    11\n",
      "dtype: int32 --->求最大值\n",
      "\n",
      "2017-01-01     0\n",
      "2017-01-06     5\n",
      "2017-01-11    10\n",
      "dtype: int32 --->求最小值\n",
      "\n",
      "2017-01-01     2.0\n",
      "2017-01-06     7.0\n",
      "2017-01-11    10.5\n",
      "dtype: float64 --->求中值\n",
      "\n",
      "2017-01-01     0\n",
      "2017-01-06     5\n",
      "2017-01-11    10\n",
      "dtype: int32 --->返回第一个值\n",
      "\n",
      "2017-01-01     4\n",
      "2017-01-06     9\n",
      "2017-01-11    11\n",
      "dtype: int32 --->返回最后一个值\n",
      "\n",
      "            open  high  low  close\n",
      "2017-01-01     0     4    0      4\n",
      "2017-01-06     5     9    5      9\n",
      "2017-01-11    10    11   10     11 --->OHLC重采样\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 重采样：.resample()\n",
    "# 创建一个以天为频率的TimeSeries，重采样为按2天为频率\n",
    "\n",
    "rng = pd.date_range('20170101',periods = 12)\n",
    "ts = pd.Series(np.arange(len(rng)),index = rng)\n",
    "print(ts)\n",
    "print(\"--------------------------------------------------------------------------\")\n",
    "\n",
    "ts_re = ts.resample('5D')\n",
    "ts_re2 = ts.resample('5D').sum()\n",
    "print(ts_re,'\\t',type(ts_re))\n",
    "print(ts_re2,'\\t',type(ts_re2))\n",
    "print(\"--------------------------------------------------------------------------\")\n",
    "# ts.resample('5D')：得到一个重采样构建器，频率改为5天\n",
    "# ts.resample('5D').sum():得到一个新的聚合后的Series，聚合方式为求和\n",
    "# freq：重采样频率 → ts.resample('5D')\n",
    "# .sum()：聚合方法\n",
    "\n",
    "print(ts.resample('5D').mean(),'--->求平均值\\n')\n",
    "print(ts.resample('5D').max(),'--->求最大值\\n')\n",
    "print(ts.resample('5D').min(),'--->求最小值\\n')\n",
    "print(ts.resample('5D').median(),'--->求中值\\n')\n",
    "print(ts.resample('5D').first(),'--->返回第一个值\\n')\n",
    "print(ts.resample('5D').last(),'--->返回最后一个值\\n')\n",
    "print(ts.resample('5D').ohlc(),'--->OHLC重采样\\n')\n",
    "# OHLC:金融领域的时间序列聚合方式 → open开盘、high最大值、low最小值、close收盘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-01-01     1\n",
      "2017-01-02     2\n",
      "2017-01-03     3\n",
      "2017-01-04     4\n",
      "2017-01-05     5\n",
      "2017-01-06     6\n",
      "2017-01-07     7\n",
      "2017-01-08     8\n",
      "2017-01-09     9\n",
      "2017-01-10    10\n",
      "2017-01-11    11\n",
      "2017-01-12    12\n",
      "Freq: D, dtype: int32\n",
      "------------------------------------------------------------------------------------------------\n",
      "2017-01-01    15\n",
      "2017-01-06    40\n",
      "2017-01-11    23\n",
      "dtype: int32 --->默认\n",
      "\n",
      "2017-01-01    15\n",
      "2017-01-06    40\n",
      "2017-01-11    23\n",
      "dtype: int32 --->left\n",
      "\n",
      "2016-12-27     1\n",
      "2017-01-01    20\n",
      "2017-01-06    45\n",
      "2017-01-11    12\n",
      "dtype: int32 --->right\n",
      "\n",
      "------------------------------------------------------------------------------------------------\n",
      "2017-01-01    15\n",
      "2017-01-06    40\n",
      "2017-01-11    23\n",
      "dtype: int32 → leftlabel\n",
      "\n",
      "2017-01-06    15\n",
      "2017-01-11    40\n",
      "2017-01-16    23\n",
      "dtype: int32 → rightlabel\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 降采样\n",
    "\n",
    "rng = pd.date_range('20170101',periods = 12)\n",
    "ts = pd.Series(np.arange(1,13),index = rng)\n",
    "print(ts)\n",
    "print(\"------------------------------------------------------------------------------------------------\")\n",
    "print(ts.resample('5D').sum(),'--->默认\\n')\n",
    "print(ts.resample('5D',closed = 'left').sum(),'--->left\\n')\n",
    "print(ts.resample('5D',closed = 'right').sum(),'--->right\\n')\n",
    "print(\"------------------------------------------------------------------------------------------------\")\n",
    "# closed：各时间段哪一端是闭合（即包含）的，默认 左闭右闭（此处要特别注意啦！！！）\n",
    "# 详解：这里values为0-11，按照5D重采样 → [1,2,3,4,5],[6,7,8,9,10],[11,12]\n",
    "# left指定间隔左边为结束 → [1,2,3,4,5],[6,7,8,9,10],[11,12]\n",
    "# right指定间隔右边为结束 → [1],[2,3,4,5,6],[7,8,9,10,11],[12]\n",
    "\n",
    "print(ts.resample('5D', label = 'left').sum(),'→ leftlabel\\n')\n",
    "print(ts.resample('5D', label = 'right').sum(),'→ rightlabel\\n')\n",
    "# label：聚合值的index，默认为取左\n",
    "# 值采样认为默认（这里closed默认）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                      a   b   c\n",
      "2017-01-01 00:00:00   0   1   2\n",
      "2017-01-01 01:00:00   3   4   5\n",
      "2017-01-01 02:00:00   6   7   8\n",
      "2017-01-01 03:00:00   9  10  11\n",
      "2017-01-01 04:00:00  12  13  14\n",
      "----------------------------------------------------------------------------------------------\n",
      "                        a     b     c\n",
      "2017-01-01 00:00:00   0.0   1.0   2.0\n",
      "2017-01-01 00:15:00   NaN   NaN   NaN\n",
      "2017-01-01 00:30:00   NaN   NaN   NaN\n",
      "2017-01-01 00:45:00   NaN   NaN   NaN\n",
      "2017-01-01 01:00:00   3.0   4.0   5.0\n",
      "2017-01-01 01:15:00   NaN   NaN   NaN\n",
      "2017-01-01 01:30:00   NaN   NaN   NaN\n",
      "2017-01-01 01:45:00   NaN   NaN   NaN\n",
      "2017-01-01 02:00:00   6.0   7.0   8.0\n",
      "2017-01-01 02:15:00   NaN   NaN   NaN\n",
      "2017-01-01 02:30:00   NaN   NaN   NaN\n",
      "2017-01-01 02:45:00   NaN   NaN   NaN\n",
      "2017-01-01 03:00:00   9.0  10.0  11.0\n",
      "2017-01-01 03:15:00   NaN   NaN   NaN\n",
      "2017-01-01 03:30:00   NaN   NaN   NaN\n",
      "2017-01-01 03:45:00   NaN   NaN   NaN\n",
      "2017-01-01 04:00:00  12.0  13.0  14.0\n",
      "                      a   b   c\n",
      "2017-01-01 00:00:00   0   1   2\n",
      "2017-01-01 00:15:00   0   1   2\n",
      "2017-01-01 00:30:00   0   1   2\n",
      "2017-01-01 00:45:00   0   1   2\n",
      "2017-01-01 01:00:00   3   4   5\n",
      "2017-01-01 01:15:00   3   4   5\n",
      "2017-01-01 01:30:00   3   4   5\n",
      "2017-01-01 01:45:00   3   4   5\n",
      "2017-01-01 02:00:00   6   7   8\n",
      "2017-01-01 02:15:00   6   7   8\n",
      "2017-01-01 02:30:00   6   7   8\n",
      "2017-01-01 02:45:00   6   7   8\n",
      "2017-01-01 03:00:00   9  10  11\n",
      "2017-01-01 03:15:00   9  10  11\n",
      "2017-01-01 03:30:00   9  10  11\n",
      "2017-01-01 03:45:00   9  10  11\n",
      "2017-01-01 04:00:00  12  13  14\n",
      "                      a   b   c\n",
      "2017-01-01 00:00:00   0   1   2\n",
      "2017-01-01 00:15:00   3   4   5\n",
      "2017-01-01 00:30:00   3   4   5\n",
      "2017-01-01 00:45:00   3   4   5\n",
      "2017-01-01 01:00:00   3   4   5\n",
      "2017-01-01 01:15:00   6   7   8\n",
      "2017-01-01 01:30:00   6   7   8\n",
      "2017-01-01 01:45:00   6   7   8\n",
      "2017-01-01 02:00:00   6   7   8\n",
      "2017-01-01 02:15:00   9  10  11\n",
      "2017-01-01 02:30:00   9  10  11\n",
      "2017-01-01 02:45:00   9  10  11\n",
      "2017-01-01 03:00:00   9  10  11\n",
      "2017-01-01 03:15:00  12  13  14\n",
      "2017-01-01 03:30:00  12  13  14\n",
      "2017-01-01 03:45:00  12  13  14\n",
      "2017-01-01 04:00:00  12  13  14\n"
     ]
    }
   ],
   "source": [
    "# 升采样及插值：\n",
    "\n",
    "rng = pd.date_range('2017/1/1 00:00:00',periods = 5,freq = 'H')\n",
    "ts = pd.DataFrame(np.arange(15).reshape(5,3),\n",
    "                 index = rng,\n",
    "                 columns = list('abc')\n",
    "                 )\n",
    "print(ts)\n",
    "print('----------------------------------------------------------------------------------------------')\n",
    "\n",
    "\n",
    "print(ts.resample('15T').asfreq())\n",
    "print(ts.resample('15T').ffill())\n",
    "print(ts.resample('15T').bfill())\n",
    "# 低频转高频，主要是如何插值\n",
    "# .asfreq()：不做填充，返回Nan\n",
    "# .ffill()：向上填充\n",
    "# .bfill()：向下填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-01     0\n",
      "2017-02     1\n",
      "2017-03     2\n",
      "2017-04     3\n",
      "2017-05     4\n",
      "2017-06     5\n",
      "2017-07     6\n",
      "2017-08     7\n",
      "2017-09     8\n",
      "2017-10     9\n",
      "2017-11    10\n",
      "2017-12    11\n",
      "2018-01    12\n",
      "Freq: M, dtype: int32\n",
      "2017-01     0\n",
      "2017-04     3\n",
      "2017-07     6\n",
      "2017-10     9\n",
      "2018-01    12\n",
      "Freq: 3M, dtype: int32\n",
      "2017-01-01     0\n",
      "2017-01-16     0\n",
      "2017-01-31     0\n",
      "2017-02-15     1\n",
      "2017-03-02     2\n",
      "2017-03-17     2\n",
      "2017-04-01     3\n",
      "2017-04-16     3\n",
      "2017-05-01     4\n",
      "2017-05-16     4\n",
      "2017-05-31     4\n",
      "2017-06-15     5\n",
      "2017-06-30     5\n",
      "2017-07-15     6\n",
      "2017-07-30     6\n",
      "2017-08-14     7\n",
      "2017-08-29     7\n",
      "2017-09-13     8\n",
      "2017-09-28     8\n",
      "2017-10-13     9\n",
      "2017-10-28     9\n",
      "2017-11-12    10\n",
      "2017-11-27    10\n",
      "2017-12-12    11\n",
      "2017-12-27    11\n",
      "2018-01-11    12\n",
      "2018-01-26    12\n",
      "Freq: 15D, dtype: int32\n"
     ]
    }
   ],
   "source": [
    "# 时期重采样--- Period\n",
    "\n",
    "prng = pd.period_range('2017','2018',freq = 'M')\n",
    "ts = pd.Series(np.arange(len(prng)),index = prng)\n",
    "print(ts)\n",
    "\n",
    "print(ts.resample('3M').ffill())  # 降采样\n",
    "print(ts.resample('15D').ffill())  # 升采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
